package com.pic.mgt.syushin.executer;

import java.util.List;

import com.pic.mgt.executer.TargetAction;
import com.pic.mgt.executer.TargetFileProcessException;
import com.pic.mgt.log.IgxLog;
import com.pic.mgt.manager.business.ApInfoBean;
import com.pic.mgt.manager.business.BasicBusinessInfoBean;
import com.pic.mgt.manager.business.BusinessManager;
import com.pic.mgt.unit.Business;
import com.pic.mgt.unit.operation.BusinessUnitOperation;
import com.pic.mgt.unit.operation.UnitOperationException;

public final class RealSyushinTargetFileProcess implements TargetAction {
	private String syushinOutboxRealPathStr;
	private BusinessUnitOperation simpleBusinessUnitOperation;
	// (送多方新增)
	private BusinessManager businessManager;
	

	@Override
	// (送多方修改)
	public void perform(Business business) throws TargetFileProcessException {
		BasicBusinessInfoBean basicBusinessInfoBean = this.businessManager.getBasicBusinessInfoBean(business.getBusinessName());
		if (null == basicBusinessInfoBean) {
			throw new TargetFileProcessException("Business '" + business.getBusinessName() + "' has not existed already.");
		}
		
		// 複製目標檔案
		List<ApInfoBean> apInfoBeanList = basicBusinessInfoBean.getSyushinBusinessApList();
		for (ApInfoBean apInfoBean : apInfoBeanList) {
			try {
				// ********************** Log **********************
				if (IgxLog.messageDetailsLog.isInfoMode()) {
					IgxLog.messageDetailsLog.info(this,
							"Copying target file '{}' to syushin outbox '{}'.",
							business.getTargetFilename(), apInfoBean.getApFolder().getAbsolutePath());
				}
				// ********************** Log **********************
				
				simpleBusinessUnitOperation.copyTargetFile(business, apInfoBean.getApFolder());
				
				// ********************** Log **********************
				if (IgxLog.messageDetailsLog.isInfoMode()) {
					IgxLog.messageDetailsLog.info(this,
							"Copying target file '{}' to syushin outbox '{}' completed.",
							business.getTargetFilename(), apInfoBean.getApFolder().getAbsolutePath());
				}
				// ********************** Log **********************
			} catch (UnitOperationException e) {
				throw new TargetFileProcessException(
						"Copying target file '" + business.getTargetFilename() + 
						"' to syushin outbox '" + apInfoBean.getApFolder().getAbsolutePath() + "' failed.", e);
			}
		}
		
		// 移除目標檔案
		try {
			// ********************** Log **********************
			if (IgxLog.messageDetailsLog.isInfoMode()) {
				IgxLog.messageDetailsLog.info(this,
						"Removing target file '{}'.", business.getTargetFilename());
			}
			// ********************** Log **********************
			
			simpleBusinessUnitOperation.removeTargetFile(business);
			
			// ********************** Log **********************
			if (IgxLog.messageDetailsLog.isInfoMode()) {
				IgxLog.messageDetailsLog.info(this,
						"Removing target file '{}' completed.", business.getTargetFilename());
			}
			// ********************** Log **********************
		} catch (UnitOperationException e) {
			throw new TargetFileProcessException("Removing target file '" + business.getTargetFile().getAbsolutePath() + "' failed.", e);
		}
	}
	
	// --- getter and setter ---

	public void setSyushinOutboxRealPathStr(String syushinOutboxRealPathStr) {
		this.syushinOutboxRealPathStr = syushinOutboxRealPathStr;
	}

	public void setSimpleBusinessUnitOperation(
			BusinessUnitOperation simpleBusinessUnitOperation) {
		this.simpleBusinessUnitOperation = simpleBusinessUnitOperation;
	}

	public void setBusinessManager(BusinessManager businessManager) {
		this.businessManager = businessManager;
	}
}
